home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / ixemlsrc.lha / ixemul / library / __swrite.c < prev    next >
C/C++ Source or Header  |  1995-12-23  |  3KB  |  99 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  __swrite.c,v 1.1.1.1 1994/04/04 04:30:12 amiga Exp
  20.  *
  21.  *  __swrite.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:30:12  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.1  1992/05/14  19:55:40  mwild
  26.  *  Initial revision
  27.  *
  28.  */
  29.  
  30. #define KERNEL
  31. #include "ixemul.h"
  32. #include "kprintf.h"
  33.  
  34. static int __do_sync_write(struct file *f, char *buf, int len)
  35. {
  36.   int err=errno, res = 0;
  37.   int omask;
  38.  
  39.   omask = syscall (SYS_sigsetmask, ~0);
  40.   __get_file (f);
  41.   /* this allows intermixing different modes.. we wouldn't need to
  42.    * wait, if we knew the user only used FSYNC-mode */
  43.   __wait_packet(&f->f_sp);
  44.  
  45.   if (len > 0)
  46.     {
  47.       /* full append-mode means, before each write do an explicit
  48.        * seek to eof */
  49.       if (f->f_flags & FAPPEND)
  50.     {
  51.       SendPacket3(f,__rwport,ACTION_SEEK,f->f_fh->fh_Arg1,0,OFFSET_END);
  52.           __wait_packet(&f->f_sp);
  53.     }
  54.  
  55.       SendPacket3(f,__rwport,ACTION_WRITE,f->f_fh->fh_Arg1,(long)buf,len);
  56.       __wait_packet(&f->f_sp);
  57.       res = LastResult(f);
  58.       if (res == -1) err = __ioerr_to_errno(LastError(f));
  59.     }
  60.  
  61.   LastResult(f) = 0;
  62.   __release_file (f);
  63.   syscall (SYS_sigsetmask, omask);
  64.   errno = err;
  65.   KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  66.   return res;
  67. }
  68.  
  69. int __sync_write(struct file *f, char *buf, int len)
  70. {
  71.   char *p;
  72.   int l, bytes, res = 0, tmp;
  73.  
  74.   if (HANDLER_NIL(f))
  75.     return len;
  76.   if (!f->f_fh->fh_Port)    /* if not interactive */
  77.     return __do_sync_write(f, buf, len);
  78.  
  79.   /* write the buffer line by line, otherwise the user isn't able to stop
  80.      the console output until the whole buffer was flushed to the console */
  81.   for (p = buf, l = 0; l < len; p += bytes, l += bytes)
  82.     {
  83.       if (p[0] == '\n')
  84.         bytes = 1;
  85.       else
  86.         for (bytes = 0; l + bytes < len && p[bytes] != '\n' && bytes < 160; bytes++) ;
  87.       if (bytes)
  88.         {
  89.           tmp = __do_sync_write(f, p, bytes);
  90.           if (tmp == -1)
  91.             return tmp;
  92.           res += tmp;
  93.           if (tmp != bytes)
  94.             return res;
  95.         }
  96.     }
  97.   return res;
  98. }
  99.